import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:plugin_secure_store/plugin_secure_store.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  static const String _alias = 'loginPubKey';
  static const String _content = '123';
  static const String _contentBig = "3082015E041011223344556677881122334455667788041011223344556677881122334455667788043411223344551122334455112233445566778811223344556677881122334411223344556677881122334411223344556677881122048201008BE571B4F98FB2EE2A21A5420B7C4531AD3A0786027E394CEEED466EB047E0A3421879A03CD8282AE5DD4B38E3A9015B90A847420B0CADC74337B0197DEAD83667CF02569AB80F1058A06372BF6DCC584D21552855249980EE8071E571ECD8EEFF4EDC776A1C2691D53192FDB4EB8BBC263412E78DBF7E61178B9B346BB1A47336D487B59EF1D3A814D8857BD2C83ACFC1BF9093338789C8423D4D7723FE46BF1E32C829FB8A10718FAB5A0CD351F413A05F2571B12088554A9284D7BCDE415EE369C31D183D91901182415BA519D6C7DDE4E23262DC4C3D53325FB0C3AE3CD4D9A00CFC7DA72BAD4D46113087D0E505766A806C424D3B6E1A2ADA9888A83AD4";
  String _pubKey = 'Unknown';
  String _resultEncrypt = 'Unknown';
  String _resultDecrypt = 'Unknown';
  String _resultBigDecrypt = 'Unknown';
  final _pluginSecureStore = PluginSecureStore.getInstance();

  @override
  void initState() {
    super.initState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> generateRSA() async {
    String pubKey;
    try {
      pubKey = await _pluginSecureStore.generateRSA(_alias) ?? '密钥生成失败';
      print('pubKey: >>>>$pubKey<<<<');
    } on PlatformException {
      pubKey = 'Failed to generate RSA key pair.';
    }

    setState(() {
      _pubKey = pubKey;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(children: [
            Text('pubKey is: $_pubKey\n'),
            Text(_resultEncrypt),
            Text(_resultDecrypt),
            Text(_resultBigDecrypt),

            TextButton(onPressed: () async {
              generateRSA();
            }, child: const Text('生成密钥')),

            TextButton(onPressed: () async {
              String? result = await _pluginSecureStore.encryptRSA(_alias, _content);
              print('encryptRSA: >>>>$result<<<<');
              setState(() {
                _resultEncrypt = result ?? '加密失败';
              });
            }, child: const Text('加密')),

            TextButton(onPressed: () async {
              if (_resultEncrypt.isEmpty || _resultEncrypt.contains('加密')) {
                _resultDecrypt = '加密数据为空, 无法解密';
                return;
              }
              String? result = await _pluginSecureStore.decryptRSA(_alias, _resultEncrypt);
              print('decryptRSA: >>>>$result<<<<');
              setState(() {
                _resultDecrypt = result ?? '加密失败';
              });
            }, child: const Text('解密')),

            TextButton(onPressed: () async {
              const data = 'n/No6t+kEVWQTMV0sj5j/aKE6FPH6JTrJ6okWfUyOwUG6HmUoMCQ/a6HtlTVxNwMEo1PyIQt7hnQcpO35JPfJpA48yzzIGjOtBL+umFCxHZ3Vl6WtYi5JvHdLvpP7gjzWazCe1B/eJWgdghBLZ9HOLSofsHtgmv5aOf4RqmLK5dTc443iXj1v+b1mC8BwJcM+U4GxlKem6ScLcobgelk554zEb3avmoYz8u7 /CNAn9Tca1UzNt/LfyAicksTfn2atjkQAOyu9VX4d2FFX7O8RtgX+QYOzWUAqjqZCallmB27WJ4O1HGt1oQg7R9bpTTPufFVnfGZp12Y4vsIWvzexTrREU6luqJmvEGI6FDQKQOhmvkPC992CWrt/NXzjzH0GpS+4sNszDNRSp47U4UMn7NnYX+xbpiRop8FCgEAq/KOufCRFTNRUPgibrCzTQSnjIh9LgDW8MKL2V9Zx8xJPjdrhJks+oAODNHfyqiTJmirDgUfKvqaIaDW/Myet5D+ViCBnE5T3pq6pHlxHQNsqun9MU0RgY6RusWdiWhrK3MYn/nFRrWdNMVqff8xGcFniFRO4rSdLrh1kxBRP5Mw05W1TEd4AUdjNzC1qG4ap3TUonZmFPe4JvR3giiYyxfNO60Pvhuw4lmq7mi4EtuszLhCOwYU1sVvbs1Ug/2+skdu8LyKDgcUE3d3pMAWtuyQGfYAMXcw+vtKLeyYlFzqwGQtNWuL7ugggaTq0mpcgalyD5hEEjxJH0lV91wAGVx52E5UX37VG1NtasmZAwPyD4AOYSq2PALK8Buu3TzD3964JJ5XDqT9krR8nHv2Ic4vJCn1wYvxwtNKfaeF+FoSHmtnVT4GjxvT5ow2/JWga0pzcXcaXfJikwbiJ3IDI45fY0dXqThr9Sw3EZmr0hABW7fzEFdfNkBgdcdFzj72vhVZdfGLD2Ow/Ulkq19VwWq52VlY2xgHusCURW1fnkYKAr7fvkUqm21bSXX/v8CoWmo6zHurnA6jZS04QsVj3t/GZX3lvgpWSUTqh02lCLsp/Ulyzhk4z8fy0UuepDAm2ch6+XgH+T6kqMcLkMtO44w4GqpIiGkvheQD9IS9PHdksw47hwz24oAu+Rg81/+n6nR5ePL+g8/VrL9dWnFUrvkq+n+Vywz+wO+rVQxEXeKpBmnAY5udGSycOJyJDYjRfy0=';
              if (data.isEmpty) {
                _resultBigDecrypt = '加密数据为空, 无法解密';
                return;
              }
              String? result = await _pluginSecureStore.decryptBigRSA(_alias, data);
              setState(() {
                _resultBigDecrypt = result ?? '解密失败';
              });
            }, child: const Text('解密大数据')),
          ])
        ),
      ),
    );
  }
}
